package com.superman.supermarket.client.activity;

import java.util.HashMap;
import java.util.Map;

import com.google.gwt.event.shared.EventBus;
import com.google.inject.Inject;
import com.superman.mvpframe.client.ajax.PostManAsyncCallBack;
import com.superman.mvpframe.client.mvp.IPageView;
import com.superman.mvpframe.client.mvp.IPageViewPresenter;
import com.superman.mvpframe.client.utils.XJsonUtil;
import com.superman.mvpframe.client.widget.mescroll.UpAndDownCallBack;
import com.superman.mvpframe.client.widget.tabbar.MyTabBarSelectEvent;
import com.superman.mvpframe.client.widget.tabbar.MyTabBarSelectEvent.MyTabBarSelectHandler;
import com.superman.supermarket.client.mvp.ShopAbstractActivity;
import com.superman.supermarket.client.view.mycouponlist.MyCouponListView;

/**
 * 我的优惠券列表
 * 
 * @author superman
 * @version 2018年8月19日下午9:55:33
 */
public class MyCouponListActivity extends ShopAbstractActivity implements UpAndDownCallBack {

	@Inject
	MyCouponListView myCouponListView;

	private int state = 1;// 默认未使用

	@Override
	public void getListDataByPage(int page) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("COUPON_STATE", state);
		map.put("PAGE_INDEX", page);
		myCouponListView.setCurrentCouponListNoUsed(state == 1);
		postman.post("/coupon/list", map, new PostManAsyncCallBack() {

			@Override
			public void onBusinessError(String statusCode) {

			}

			@Override
			public void onBusinessOK(String response) {
				myCouponListView.getPullToRefreshView().endBySize(XJsonUtil.getList(response), XJsonUtil.getCount(response));
			}
		});
	}

	@Override
	public IPageView getView() {
		return myCouponListView;
	}

	@Override
	public IPageViewPresenter getViewPresenter() {
		// TODO Auto-generated method stub
		return null;
	}

	private void initTabBar() {
		myCouponListView.getTabBar().addMyTabBarSelectHandler(new MyTabBarSelectHandler() {

			@Override
			public void onTabBarSelected(MyTabBarSelectEvent event) {
				int index = event.getIndex();
				if (index == 0) {
					state = 1;
				} else {
					state = 4;
				}
				myCouponListView.getPullToRefreshView().clearListAndResetScroll();
			}
		});

		myCouponListView.getTabBar().select(0, false);
	}

	@Override
	public void onActivity(EventBus eventBus) {
		// TODO Auto-generated method stub

	}

	@Override
	public void popToRefresh() {
		// TODO Auto-generated method stub

	}

	@Override
	public void popToRefresh(Object contextData) {
		// TODO Auto-generated method stub

	}

	@Override
	public void pushToRefresh() {
		myCouponListView.getPullToRefreshView().initMeScroll(this, "2.533333rem", "0rem");
		initTabBar();
	}

}
